package com.mogujie.tt.ui.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mogujie.tt.DB.sp.GesturespasswordSp;
import com.mogujie.tt.DB.sp.LoginSp;
import com.mogujie.tt.R;
import com.mogujie.tt.app.IMApplication;
import com.mogujie.tt.ui.activity.DigitalCodeActivity;
import com.mogujie.tt.ui.activity.UnlockGesturePasswordActivity;
import com.mogujie.tt.ui.widget.AddPopWindow;
import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * Created by user on 2015/11/2.
 */
public abstract class QZBaseActivity extends Activity {

    protected ImageView topLeftBtn;
    protected ImageView topRightBtn;
    protected TextView topTitleTxt;
    protected TextView letTitleTxt;
    protected ViewGroup topBar;
    protected ViewGroup secTopBar ;
    protected ViewGroup topContentView;
    protected LinearLayout baseRoot;
    protected float x1, y1, x2, y2 = 0;

    protected ImageView topLeftOutButton;
//    protected View topLeftView;
    protected TextView topCenterTxt;

    protected FrameLayout secFrameLayout;
    protected TextView  secLeftViewText;
    protected TextView  secCenterViewText;
    protected TextView  secRightViewText;

    protected EditText topRightSearch;
    protected ImageView topRightAdd;
    protected boolean isAutoPlay=false;
    protected boolean Num = false;
    protected boolean Ges = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题

        topContentView = (ViewGroup) LayoutInflater.from(this).inflate(
                R.layout.qz_activity_base, null);
        topBar = (ViewGroup) topContentView.findViewById(R.id.topbar);
        secTopBar = (ViewGroup) topContentView.findViewById(R.id.sectopbar);
        topTitleTxt = (TextView) topContentView.findViewById(R.id.base_activity_title);
        topLeftBtn = (ImageView) topContentView.findViewById(R.id.left_btn);
        topRightBtn = (ImageView) topContentView.findViewById(R.id.right_btn);
        letTitleTxt = (TextView) topContentView.findViewById(R.id.left_txt);
        baseRoot = (LinearLayout)topContentView.findViewById(R.id.act_base_root);

        topTitleTxt.setVisibility(View.GONE);
        topRightBtn.setVisibility(View.GONE);
        letTitleTxt.setVisibility(View.GONE);
        topLeftBtn.setVisibility(View.GONE);
        //新添加的三个控件
        topLeftOutButton = (ImageView)topContentView.findViewById(R.id.left_out_button);
//        topLeftView = (View)topContentView.findViewById(R.id.left_view);
        topCenterTxt =(TextView)topContentView.findViewById(R.id.center_txt);

        topRightSearch =(EditText)topContentView.findViewById(R.id.top_right_search);//搜索框
        topRightAdd =(ImageView)topContentView.findViewById(R.id.top_right_add);//添加好友

        topLeftOutButton.setVisibility(View.GONE);
 //       topLeftView.setVisibility(View.GONE);
        topCenterTxt.setVisibility(View.GONE);
        topRightSearch.setVisibility(View.GONE);
        topRightAdd.setVisibility(View.GONE);

        //第二行显示
        secFrameLayout =(FrameLayout)topContentView.findViewById(R.id.select_level_button);
        secLeftViewText =(TextView)topContentView.findViewById(R.id.level_button1);
        secCenterViewText =(TextView)topContentView.findViewById(R.id.level_button2);
        secRightViewText =(TextView)topContentView.findViewById(R.id.level_button3);
        secFrameLayout.setVisibility(View.GONE);
        secLeftViewText.setVisibility(View.GONE);
        secCenterViewText.setVisibility(View.GONE);
        secRightViewText.setVisibility(View.GONE);

        topBar.setVisibility(View.GONE);

        setContentView(topContentView);

    /*    if (android.os.Build.VERSION.SDK_INT > 18) {
            //透明状态栏
         //  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }*/

        if(isPad()){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏
        }else{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        }

    }



    /**
     * 设置添加按钮
     */
    protected void  setTopRightAdd(){
        topRightAdd.setVisibility(View.VISIBLE);
        topRightAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddPopWindow addPopWindow = new AddPopWindow(QZBaseActivity.this);
                addPopWindow.showPopupWindow(topRightAdd);
            }
        });
    }

    protected void setLeftText(String text) {
        if (null == text) {
            return;
        }
        letTitleTxt.setText(text);
        letTitleTxt.setVisibility(View.VISIBLE);
    }

    protected void setTitle(String title) {
        if (title == null) {
            return;
        }
        if (title.length() > 12) {
            title = title.substring(0, 11) + "...";
        }
        topTitleTxt.setText(title);
        topTitleTxt.setVisibility(View.VISIBLE);
    }

    @Override
    public void setTitle(int id) {
        String strTitle = getResources().getString(id);
        setTitle(strTitle);
    }

    protected void setLeftButton(int resID) {
        if (resID <= 0) {
            return;
        }

        topLeftBtn.setImageResource(resID);
        topLeftBtn.setVisibility(View.VISIBLE);
    }

    protected void setRightButton(int resID) {
        if (resID <= 0) {
            return;
        }

        topRightBtn.setImageResource(resID);
        topRightBtn.setVisibility(View.VISIBLE);
    }

    /**
     * 设置顶部条显示
     */
    protected void setTopBarDisplay() {
        secTopBar.setVisibility(View.GONE);
        topBar.setVisibility(View.VISIBLE);
        topLeftOutButton.setVisibility(View.VISIBLE);

    }

    /**
     * 设置中间为什么字
     * @param text
     */
    protected void setCenterText(String text) {
        if (null == text) {
            return;
        }
        topCenterTxt.setText(text);
        topCenterTxt.setVisibility(View.VISIBLE);
    }
    /**
     * 显示搜索栏
     */
    protected void setSearchBar(){
        topRightSearch.setText("");
        topRightSearch.setVisibility(View.VISIBLE);
    }

    /**
     * 设置按钮的图案背景
     * @param resID
     */
    protected void setLeftOutButton(int resID){
        if (resID <= 0) {
            return;
        }
        topLeftOutButton.setBackgroundResource(resID);
        topLeftOutButton.setVisibility(View.VISIBLE);
    }
 /*   *//**
     * 设置中间条显示
     *//*
    protected void settopLeftView(){
        topLeftView.setVisibility(View.VISIBLE);
    }*/

    protected void setseclayout(int choose){
        secFrameLayout.setVisibility(View.VISIBLE);
        switch (choose){
            case 0:
                secLeftViewText.setVisibility(View.VISIBLE);
                secLeftViewText.setClickable(true);

                secCenterViewText.setVisibility(View.VISIBLE);
                //   secCenterViewText.setBackgroundColor(Color.rgb(9, 255, 31));//绿色
                secCenterViewText.setBackgroundResource(R.drawable.textviewgreen2);
                secCenterViewText.setClickable(false);

                secRightViewText.setVisibility(View.GONE);
                break;
            case 1:
                secLeftViewText.setVisibility(View.VISIBLE);
                secLeftViewText.setClickable(true);

                secCenterViewText.setVisibility(View.VISIBLE);
                secCenterViewText.setClickable(true);
                //    secCenterViewText.setBackgroundColor(Color.rgb(255, 255, 255));//白色
                secCenterViewText.setBackgroundResource(R.drawable.textviewwhite3);

                secRightViewText.setVisibility(View.VISIBLE);
                //       secRightViewText.setBackgroundColor(Color.rgb(9, 255, 31));//绿色
                secRightViewText.setBackgroundResource(R.drawable.textviewgreen2);
                break;
            case 2:
                secFrameLayout.setVisibility(View.GONE);
                secLeftViewText.setVisibility(View.GONE);
                secCenterViewText.setVisibility(View.GONE);
                secRightViewText.setVisibility(View.GONE);
                break;
            default:break;
        }
    }

    protected void setSecLeftViewText(String text) {
        if (null == text) {
            return;
        }
        secLeftViewText.setText(text);
    }
    protected void setSecCenterViewText(String text) {
        if (null == text) {
            return;
        }
        secCenterViewText.setText(text);
    }
    protected void setSecRightViewText(String text) {
        if (null == text) {
            return;
        }
        secRightViewText.setText(text);
    }


    @Override
    public void onStart() {
        super.onStart();
        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(testReceiver, filter);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (!IMApplication.getInstance().isAppOnForeground()) {
            IMApplication.getInstance().isActive = true;
        }
    }

    @Override
    public void onResume() {
        NoGWho();
        super.onResume();
    }


    public void onDest(){
        unregisterReceiver(testReceiver);
    }

    public void NoGWho(){

        String sign=GesturespasswordSp.get(this,GesturespasswordSp.getNUM(),"");
        if ("".equals(sign)||sign==null){
            Num = false;
        }else{
            if (sign.trim().length()==6){
                Num = true;
            }else
                Num = false;
        }
        if (IMApplication.getInstance().getLockPatternUtils().savedPatternExists()){
            Ges = true;
        }else
            Ges = false;

        if (Num||Ges) {
            if (IMApplication.getInstance().isActive) {
                IMApplication.getInstance().isActive = false;
                if (Num) {
                    startActivity(new Intent(QZBaseActivity.this, DigitalCodeActivity.class));
                } else if (Ges) {
                    startActivity(new Intent(QZBaseActivity.this, UnlockGesturePasswordActivity.class));
                }
            }
        }
        IMApplication.getInstance().isActive = false;
    }

    public void NoGWho1(){
        String sign=GesturespasswordSp.get(this,GesturespasswordSp.getNUM(),"");
        if ("".equals(sign)||sign==null){
            Num = false;
        }else{
            if (sign.trim().length()==6){
                Num = true;
            }else
                Num = false;
        }
        if (IMApplication.getInstance().getLockPatternUtils().savedPatternExists()){
            Ges = true;
        }else
            Ges = false;
        if (Num||Ges) {
            if (Num) {
                startActivity(new Intent(QZBaseActivity.this, DigitalCodeActivity.class));
            } else if (Ges) {
                startActivity(new Intent(QZBaseActivity.this, UnlockGesturePasswordActivity.class));
            }
        }
    }

    public BroadcastReceiver testReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case "android.intent.action.SCREEN_OFF":
                case "android.intent.action.SCREEN_SCREEN_ON":
                case "android.intent.action.USER_PRESENT":
                    if (!IMApplication.getInstance().isActive) {
                        NoGWho1();
                    }
                    break;
            }
        }
    };
    /**
     * 判断是否为平板
     *
     * @return
     */
    private boolean isPad() {
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        float screenWidth = display.getWidth();
        // 屏幕高度
        float screenHeight = display.getHeight();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
        // 屏幕尺寸
        double screenInches = Math.sqrt(x + y);
        // 大于6尺寸则为Pad
        if (screenInches >= 6.0) {
            return true;
        }
        return false;
    }

}
